package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mvel2.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.Notice;
import xyz.hubery.model.R;
import xyz.hubery.service.NoticeService;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "公告管理")
@RestController
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping
    @ApiOperation("分页查询公告")
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> queryPage(Page<Notice> page,String title,String startTime,String endTime,Integer status) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(
                noticeService.page(page,new LambdaQueryWrapper<Notice>()
                        .like(!StringUtils.isEmpty(title),Notice::getTitle,title)
                        .between(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime),Notice::getCreated,startTime,endTime + " 23:59:59")
                        .eq(status != null ,Notice::getStatus,status)
                )
        );
    }


    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('notice_delete')")
    @ApiOperation("删除公告")
    public R delete(@RequestBody @NotNull List<Long> ids) {
        boolean b = noticeService.removeByIds(ids);
        if(b){
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/updateStatus")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiOperation("启用/禁用一个公告")
    public R updateStatus(Long id,Integer status) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setStatus(status);
        boolean b = noticeService.updateById(notice);
        if(b){
            return R.ok();
        }
        return R.fail();
    }



    @PostMapping
    @PreAuthorize("hasAuthority('notice_create')")
    @ApiOperation("新增公告")
    public R add(@RequestBody Notice notice) {
        notice.setStatus(1);
        if(noticeService.save(notice)) {
            return R.ok();
        }
        return R.fail();
    }

    @PatchMapping
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiOperation("修改公告")
    public R update(@RequestBody Notice notice) {
        if(noticeService.updateById(notice)) {
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/simple")
    @ApiOperation("分页查询所有启用的公告")
    public R<Page<Notice>> findAllSimple(Page<Notice> page) {
        page.addOrder(OrderItem.descs("sort"));
        Page<Notice> noticePage = noticeService.page(page,new LambdaQueryWrapper<Notice>().eq(Notice::getStatus,1));
        return R.ok(noticePage);
    }

    @GetMapping("/simple/{id}")
    @ApiOperation("根据ID查询公告详情")
    public R<Notice> findSimpleById(@PathVariable Long id) {
        Notice notice = noticeService.getById(id);
        return R.ok(notice);
    }

}
